/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.zzz.view.teacherControl;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

import org.zzz.controller.AttendanceController;
import org.zzz.controller.ClassController;
import org.zzz.entity.Class;
import org.zzz.entity.Course;
import org.zzz.entity.vo.AttendanceShowVo;
import org.zzz.utils.Result;
import org.zzz.view.util.UiUtil;

import java.awt.*;
import java.util.List;

/**
 *
 * @author AFun
 */
public class teacherSearchFrame extends javax.swing.JFrame {

    /**
     * Creates new form studentFrame
     */
    public teacherSearchFrame(String date,String name) {
        initComponents();
        init(date,name);         //初始化
    }
    
    public teacherSearchFrame(){
        initComponents();
        this.setTitle("请选择您所要进行的查询");
        UiUtil.setFrameCenter(this);
        this.setResizable(false);
        setClass();
    }

    public teacherSearchFrame(teacherViewFrame tf){
        this.tf=tf;
        initComponents();
        this.setTitle("请选择您所要进行的查询");
        UiUtil.setFrameCenter(this);
        this.setResizable(false);
        setClass();
    }
    
    private void init(String date,String name){
        this.setTitle("请选择您所要进行的查询");
        UiUtil.setFrameCenter(this);
        this.setResizable(false);
        setClass();
    }

    private void setClass(){
        Rectangle bounds = this.jTextField2.getBounds();
        this.m_Class.setBounds(bounds);
        this.m_Class.setVisible(false);
        //获取所有班级并加入组合框内
        //获取全部班级
        ClassController classController = new ClassController();
        Result<List<Class>> listResult = classController.listClass();
        List<Class> classObject = listResult.getData();

        int index=0;
        for (Class aClass : classObject) {
            this.m_Class.insertItemAt(aClass,index++);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jComboBox2 = new javax.swing.JComboBox<>();
        m_Class = new javax.swing.JComboBox<>();
        jButton2 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel3.setText("信息");

        jLabel4.setText("类型");

        jTextField2.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTextField2MouseClicked(evt);
            }
        });

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "日期", "班级", "姓名" }));
        jComboBox2.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jComboBox2MouseClicked(evt);
            }
        });
        jComboBox2.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox2ActionPerformed(evt);
            }
        });

        m_Class.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                m_ClassActionPerformed(evt);
            }
        });

        jButton2.setText("确定");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton1.setText("关闭");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                                .addGap(18, 18, 18)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                        .addComponent(jLabel4)
                                                        .addComponent(jLabel3))
                                                .addGap(18, 18, 18)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addGroup(layout.createSequentialGroup()
                                                                .addComponent(m_Class, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE))
                                                        .addGroup(layout.createSequentialGroup()
                                                                .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addGap(52, 52, 52)
                                                                .addComponent(jButton1))))
                                        .addGroup(layout.createSequentialGroup()
                                                .addGap(34, 34, 34)
                                                .addComponent(jButton2)))
                                .addContainerGap(21, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGap(28, 28, 28)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel3)
                                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(m_Class, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(24, 24, 24)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel4)
                                        .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(35, 35, 35)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jButton2)
                                        .addComponent(jButton1))
                                .addContainerGap(26, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    private void jTextField2MouseClicked(java.awt.event.MouseEvent evt) {
        // TODO add your handling code here:


    }

    private void jComboBox2MouseClicked(java.awt.event.MouseEvent evt) {
        // TODO add your handling code here:
    }

    private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        //遍历组合框数据
        if(this.jComboBox2.getSelectedItem().toString().equals("班级")){
            this.jTextField2.setVisible(false);
            this.m_Class.setVisible(true);
        }else{
            this.jTextField2.setVisible(true);
            this.m_Class.setVisible(false);
        }
    }

    private void m_ClassActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    }


    //关闭按钮
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:

        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    //查询按钮
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:

        //查询
//        DefaultTableModel model = (DefaultTableModel)this.stats.getModel();
//        while(0 != model.getRowCount()){
//            model.removeRow(0);
//        }



//        AttendanceController attendanceController = new AttendanceController();
//        Result<List<AttendanceShowVo>> listResult = null;
//        if("日期".equals(selectedItem)){
//            listResult= attendanceController.listAttendance(tf.teacherId);
//        }else if("姓名".equals(selectedItem){
//            listResult=attendanceController.listAttendance(tf.teacherId);
//        }
        Integer type = 0;
        String selectedItem = (String)this.jComboBox2.getSelectedItem();
        if ("日期".equals(selectedItem)){
            type=1;
//            new teacherViewFrame(this.stats,"333",1,this.jTextField2.getText()).setVisible(true);
        }else if ("姓名".equals(selectedItem)){
            type=2;
//            new teacherViewFrame(this.stats,"333",2,this.jTextField2.getText()).setVisible(true);
        }else{
            //班级
            type=3;
//            new teacherViewFrame(this.stats,"333",3,this.jTextField2.getText()).setVisible(true);
        }
        String value = null;
        this.tf.removeTable();
        if (type != 3){
            //姓名，日期
            value = this.jTextField2.getText();
        }else{
            //班级
            Class selectedItem1 = (Class)m_Class.getSelectedItem();
            value = selectedItem1.getId();
        }
        this.tf.setTable(type,value);
        JOptionPane.showMessageDialog(null, "查询成功！");
        this.dispose();


        //查询失败，弹出对话框
//        JOptionPane.showMessageDialog(null, "查无此人！");
        //查询成功，将数据引入
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(teacherSearchFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(teacherSearchFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(teacherSearchFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(teacherSearchFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new teacherSearchFrame().setVisible(true);
            }
        });
    }

    private teacherViewFrame tf;

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox<String> jComboBox2;
    private javax.swing.JComboBox<Course> jComboBox1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JComboBox<Class> m_Class;
    // End of variables declaration//GEN-END:variables
//    private javax.swing.JTable stats;
    private String teacherId;
}
